function fig = fm_dir(flag)
% FM_DIR create GUI for data format conversion
%
% HDL = FM_DIR(FLAG)
%       FLAG = 0: DATA CONVERISON
%       FLAG = 1: OPEN DATA FILE
%       FLAG = 2: OPEN PERTURBATION FILE
%       FLAG = 3: OPEN ANY FILE
%
%see also FM_DIRSET
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Update:    31-Jul-2003
%Update:    07-Oct-2003
%Version:   1.1.0
%
%E-mail:    federico.milano@ucd.ie
%Web-site:  faraday1.ucd.ie/psat.html
%
% Copyright (C) 2002-2019 Federico Milano

global Settings Path Theme Fig File

if ishandle(Fig.dir), figure(Fig.dir), return, end

frompsat = {'IEEE CDF'; 'EPRI-WECC'; 'ODM'};

h0 = figure( ...
    'Color',Theme.color01, ...
    'Units', 'normalized', ...
    'ColorMap', [], ...
    'ResizeFcn','fm_dirset chformat', ...
    'CreateFcn','Fig.dir = gcf;', ...
    'DeleteFcn','Fig.dir = -1;', ...
    'FileName','fm_dir', ...
    'MenuBar','none', ...
    'Name','Data Conversion', ...
    'NumberTitle','off', ...
    'PaperPosition',[18 180 576 432], ...
    'PaperUnits','points', ...
    'Position',sizefig(1.1*0.5375,1.1*0.5068), ...
    'ToolBar','none');

if Settings.hostver < 7.05
  set(h0,'ShareColors','on')
end

fm_set colormap

if flag == 1, set(h0,'Name','Load Data File'), end
if flag == 2, set(h0,'Name','Load Perturbation File'), end
if flag == 3, set(h0,'Name','Open File'), end

h1 = axes( ...
    'Parent',h0, ...
    'Box','on', ...
    'CameraUpVector',[0 1 0], ...
    'CameraUpVectorMode','manual', ...
    'Color',Theme.color04, ...
    'ColorOrder',Settings.color, ...
    'SortMethod','depth', ...
    'Layer','top', ...
    'NextPlot','add', ...
    'Position',[0.7393    0.4016    0.1931    0.3411], ...
    'Tag','Axes1', ...
    'XColor',[0 0 0], ...
    'XLim',[0.5 117.5], ...
    'XLimMode','manual', ...
    'XTickLabelMode','manual', ...
    'XTickMode','manual', ...
    'YColor',[0 0 0], ...
    'YDir','reverse', ...
    'YLim',[0.5 147.5], ...
    'YLimMode','manual', ...
    'YTickLabelMode','manual', ...
    'YTickMode','manual', ...
    'ZColor',[0 0 0]);

a = imread([Path.images,'logo_ieee.jpg'],'jpg');

h2 = image( ...
    'Parent',h1, ...
    'CData',a, ...
    'Tag','Axes1Image1', ...
    'XData',[1 117], ...
    'YData',[1 147]);

    %'Erase','none', ...

h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color02, ...
    'Callback','fm_dirset preview', ...
    'Position',[0.70349 0.77842 0.1 0.05], ...
    'String','Preview', ...
    'Visible','off', ...
    'Enable','off', ...
    'Tag','PushbuttonPreview');

h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color04, ...
    'Callback','fm_dirset diredit', ...
    'CreateFcn', 'fm_dirset editinit', ...
    'FontName',Theme.font01, ...
    'HorizontalAlignment','left', ...
    'Position',[0.18023 0.88054 0.7 0.052023], ...
    'String',pwd, ...
    'Style','edit', ...
    'Tag','EditText1');

if strcmp(Settings.platform,'MAC')
  set(h1, ...
      'Position',[0.18023 0.88054 0.7 0.062023], ...
      'HorizontalAlignment','center')
end

h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color03, ...
    'Callback','fm_dirset convert', ...
    'FontWeight','bold', ...
    'ForegroundColor',Theme.color09, ...
    'Position',[0.70349 0.25626 0.26163 0.090559], ...
    'String','Convert', ...
    'Tag','Pushbutton1');

if flag, set(h1,'String','Load','Callback','fm_dirset openfile'), end

hpop = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color04, ...
    'ForegroundColor',Theme.color05, ...
    'Callback','fm_dirset chformat', ...
    'Position',[0.039244 0.33333-0.015 0.27035 0.055462], ...
    'String',fm_dirset('formatlist'), ...
    'UserData', flag, ...
    'Style','popupmenu', ...
    'Tag','PopupMenu1', ...
    'Value',1);

str = 'if strcmp(get(Fig.dir,''SelectionType''),''open''),fm_dirset view,end';

h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color04, ...
    'ButtonDownFcn','fm_dirset convert', ...
    'Callback', str, ...
    'FontName',Theme.font01, ...
    'ForegroundColor',Theme.color07, ...
    'Max',201, ...
    'Position',[0.37064 0.050096 0.2718 0.69364], ...
    'Style','listbox', ...
    'String',cell(0,1), ...
    'ListboxTop',1, ...
    'Tag','Listbox2', ...
    'Value',1);

if flag, set(h1,'Max',0,'ButtonDownFcn','fm_dirset openfile'), end

% checkboxes
h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color01, ...
    'Position',[0.039244 0.050096 0.27035 0.055462], ...
    'String','Silent conversion', ...
    'TooltipString','Do not ask for additional files for data conversion', ...
    'Style','checkbox', ...
    'Tag','CheckboxSilent', ...
    'Value',0);

h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color01, ...
    'Position',[0.039244 0.12 0.27035 0.055462], ...
    'String','Verbose conversion', ...
    'TooltipString','Print additional comments during data conversion', ...
    'Style','checkbox', ...
    'Tag','CheckboxVerbose', ...
    'Value',0);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color01, ...
               'HorizontalAlignment','left', ...
               'Position',[0.039244  0.25  0.2718  0.036609], ...
               'Enable','off', ...
               'String','Translate PSAT file to:', ...
               'Style','text', ...
               'Tag','StaticText2');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color01, ...
               'Position',[0.039244 0.20424-0.015 0.27035 0.055462], ...
               'Enable','off', ...
               'String',frompsat, ...
               'Style','popupmenu', ...
               'Tag','Checkbox2', ...
	       'Value', 1);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color01, ...
               'HorizontalAlignment','left', ...
               'Position',[0.039244     0.88247     0.13081    0.050096], ...
               'String','Current path:', ...
               'Style','text', ...
               'Tag','StaticText1');

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color01, ...
               'HorizontalAlignment','left', ...
               'Position',[0.039244     0.77842     0.27035    0.050096], ...
               'String','Folders in current path:', ...
               'Style','text', ...
               'Tag','StaticText1');

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color01, ...
               'HorizontalAlignment','left', ...
               'Position',[0.039244     0.38536      0.2718    0.036609], ...
               'String','Filters:', ...
               'Style','text', ...
               'Tag','StaticText1');

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color01, ...
               'HorizontalAlignment','left', ...
               'Position',[0.37064     0.77842      0.2718    0.050096], ...
               'String','Files in current path:', ...
               'Style','text', ...
               'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color04, ...
               'ButtonDownFcn','fm_dirset changedir', ...
               'Callback', 'fm_dirset dirsel', ...
               'CreateFcn','fm_dirset dirinit', ...
               'FontName',Theme.font01, ...
               'ForegroundColor',Theme.color05, ...
               'Max',201, ...
               'Position',[0.039244 0.45279 0.2718 0.29094], ...
               'String', cell(0,1), ...
               'ListboxTop',1, ...
               'Style','listbox', ...
               'Tag','Listbox1', ...
               'Value',1);

h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_dirset view', ...
               'Position',[0.70349     0.15222     0.26163    0.090559], ...
               'String','View File', ...
               'Tag','Pushbutton2');
h1 = uicontrol('Parent',h0, ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback', 'close(gcf);', ...
               'Position',[0.70349    0.050096     0.26163    0.090559], ...
               'String','Close', ...
               'Tag','Pushbutton3');

if flag, set(h1,'Callback','fm_dirset cancel','String','Cancel'), end

h1 = uicontrol('Parent',h0, ...
               'CData', fm_mat('misc_folder'), ...
               'Units', 'normalized', ...
               'BackgroundColor',Theme.color02, ...
               'Callback','fm_dirset getfolder', ...
               'Position',[0.9164 0.875 0.065/1.333 0.065], ...
               'TooltipString','Get folder', ...
               'Tag','Pushbutton4');

% Menu File
h1 = uimenu('Parent',h0, ...
	    'Label','File', ...
	    'Tag','U2');
h2 = uimenu('Parent',h1, ...
	    'Callback','close(gcf);', ...
	    'Label','Close', ...
	    'Tag','S24', ...
	    'Accelerator','x');

% Menu Edit
h1 = uimenu('Parent',h0, ...
	    'Label','Edit', ...
	    'Tag','U3');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_dirset convert', ...
	    'Label','Convert', ...
	    'Tag','S24', ...
	    'Accelerator','c');
h2 = uimenu('Parent',h1,'Callback', ...
	    'hdl = findobj(Fig.dir,''Tag'',''Listbox1''); set(hdl,''Value'',1), fm_dirset changedir', ...
	    'Label','Refresh Folder', ...
	    'Tag','S24', ...
	    'Accelerator','r', ...
	    'Separator','on');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_dirset changedir', ...
	    'Label','Change Folder', ...
	    'Tag','S24', ...
	    'Accelerator','z');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_dirset getfolder', ...
	    'Label','Get Folder', ...
	    'Tag','S24', ...
	    'Accelerator','g');

h2 = uimenu('Parent',h1, ...
	    'Callback', 'hdl = findobj(Fig.dir,''Tag'',''Listbox1''); set(hdl,''Value'',2), fm_dirset changedir', ...
	    'Label','Back Folder', ...
	    'Tag','S24', ...
	    'Accelerator','b');

% Menu View
h1 = uimenu('Parent',h0, ...
	    'Label','View', ...
	    'Tag','U3');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_dirset view', ...
	    'Label','View File', ...
	    'Tag','S24', ...
	    'Accelerator','v');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_dirset preview', ...
	    'Label','Preview Simulink model', ...
	    'Tag','Preview', ...
	    'Accelerator','p');
h2 = uimenu('Parent',h1, ...
	    'Callback','fm_tviewer', ...
	    'Label','Text Viewer', ...
	    'Tag','OTV', ...
	    'Accelerator','t');

switch flag
 case 0
  set(hpop,'Value',Settings.format)
 case 1
  set(hpop,'Value',Settings.format)
 case 2
  set(hpop,'Value',3,'Enable','inactive')
 case 3
  string = get(hpop,'String');
  set(hpop,'Value',length(string),'Enable','inactive')
end
fm_dirset chformat

if nargout > 0, fig = h0; end
